<template>
  <ContentWrap>
    <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="150px" :disabled="disabled">
      <el-row>
        <el-col :span="12">
          <el-form-item label="级别" prop="resLevel">
            <el-select v-model="formData.resLevel" placeholder="请选择级别" clearable>
              <el-option v-for="item in resLevelOption" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="级别名称" prop="resLevelName" label-width="220px">
            <el-input v-model="formData.resLevelName" clearable placeholder="请输入级别" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="区域" prop="regionId">
            <el-select v-model="formData.regionId" placeholder="请选择区域" clearable>
              <el-option v-for="item in regionList" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="海岸线及边境区域外扩距离" prop="distance" label-width="220px">
            <el-input v-model.number="formData.distance" style="width:200px" clearable onkeyup="value=value.replace(/[^\d]+/g,'')"/>
            &emsp;千米
          </el-form-item>
        </el-col>

        <el-col :span="16">
          <el-form-item label="震级范围">
            <el-row :gutter="5" style="width: 100%;">
              <el-col :span="7">
                <el-input-number v-model="formData.range1" :precision="1" :step="1" controls-position="right"
                  style="width:100%" clearable />
              </el-col>
              <el-col :span="4">
                <el-select v-model="formData.symbol1" clearable>
                  <el-option v-for="item in symbolOption" :label="item.label" :value="item.value" />
                </el-select>
              </el-col>
              <el-col :span="2" style="text-align: center">
                震级
              </el-col>
              <el-col :span="4">
                <el-select v-model="formData.symbol2" clearable>
                  <el-option v-for="item in symbol1Option" :label="item.label" :value="item.value" />
                </el-select>
              </el-col>
              <el-col :span="7">
                <el-input-number v-model="formData.range2" :precision="1" :step="1" controls-position="right"
                  style="width:100%" clearable />
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>

        <el-col :span="16">
          <el-form-item label="震源深度范围">
            <el-row :gutter="5" style="width: 100%;">
              <el-col :span="7">
                <el-input v-model.number="formData.depth1" style="width:100%" clearable onkeyup="value=value.replace(/[^\d]+/g,'')"/>
              </el-col>
              <el-col :span="4">
                <el-select v-model="formData.symbol3" clearable>
                  <el-option v-for="item in symbolOption" :label="item.label" :value="item.value" />
                </el-select>
              </el-col>

              <el-col :span="2" style="text-align: center">
                深度
              </el-col>
              <el-col :span="4">
                <el-select v-model="formData.symbol4" clearable>
                  <el-option v-for="item in symbol1Option" :label="item.label" :value="item.value" />
                </el-select>
              </el-col>
              <el-col :span="7" style="position: relative;">
                <el-input v-model.number="formData.depth2" style="width:100%" clearable onkeyup="value=value.replace(/[^\d]+/g,'')"/>
                <span style="position: absolute;display: inline-block;right: -40px;">&emsp;千米</span>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>

        <el-col :span="16">
          <el-form-item label="最大烈度范围">
            <el-row :gutter="5" style="width: 100%;">
              <el-col :span="7">
                <el-select v-model="formData.intensity1" style="width:100%" clearable>
                  <el-option v-for="item in intensityOption" :label="item.label" :value="item.value" />
                </el-select>
              </el-col>
              <el-col :span="4">
                <el-select v-model="formData.symbol5" clearable>
                  <el-option v-for="item in symbolOption" :label="item.label" :value="item.value" />
                </el-select>
              </el-col>
              <el-col :span="2" style="text-align: center;">
                烈度
              </el-col>
              <el-col :span="4">
                <el-select v-model="formData.symbol6" clearable>
                  <el-option v-for="item in symbol1Option" :label="item.label" :value="item.value" />
                </el-select>
              </el-col>
              <el-col :span="7">
                <el-select v-model="formData.intensity2" style="width:100%" clearable>
                  <el-option v-for="item in intensityOption" :label="item.label" :value="item.value" />
                </el-select>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>


        <el-col :span="16">
          <el-form-item label="人口密度范围">
            <el-row :gutter="5" style="width: 100%;">
              <el-col :span="7">
                <el-input v-model.number="formData.population1" style="width:100%" clearable onkeyup="value=value.replace(/[^\d]+/g,'')"/>
              </el-col>
              <el-col :span="4">
                <el-select v-model="formData.symbol7" clearable>
                  <el-option v-for="item in symbolOption" :label="item.label" :value="item.value" />
                </el-select>
              </el-col>
              <el-col :span="2" style="text-align: center">
                密度
              </el-col>
              <el-col :span="4">
                <el-select v-model="formData.symbol8" clearable>
                  <el-option v-for="item in symbol1Option" :label="item.label" :value="item.value" />
                </el-select>
              </el-col>
              <el-col :span="7" style="position: relative;">
                <el-input v-model.number="formData.population2" style="width:100%" clearable onkeyup="value=value.replace(/[^\d]+/g,'')"/>
                <span style="position: absolute;display: inline-block;right: -60px;">&emsp;人/千米</span>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="启用" prop="identifying">
            <el-switch v-model="formData.identifying" :active-value="0" :inactive-value="1" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div style="text-align: center">
          <el-button :disabled="formLoading" type="primary" @click="submitForm" v-if="!disabled">确 定</el-button>
          <el-button @click="setClose()">取 消</el-button>
    </div>
  </ContentWrap>
</template>
<script lang="ts" setup>
import * as levelApi from '@/api/responseLevel/levelSetting'
defineOptions({ name: 'SettingForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: null,
  resLevel: '',
  resLevelName: '',
  regionId: '',
  distance: 0,
  range1: undefined,
  symbol1: 0,
  symbol2: 0,
  range2: undefined,
  depth1: undefined,
  symbol3: 0,
  symbol4: 0,
  depth2: undefined,
  intensity1: '',
  symbol5: 0,
  symbol6: 0,
  intensity2: '',
  population1: undefined,
  symbol7: 0,
  symbol8: 0,
  population2: undefined,
  identifying: 0,
})
const resLevelOption = ref([
  { label: 'I', value: 1 }, { label: 'II', value: 2 }, { label: 'III', value: 3 }, { label: 'IV', value: 4 },
])
const symbol1Option = ref([
  { label: '≤', value: 0 }, { label: '<', value: 1 }, { label: '=', value: 2 },
])
const symbolOption = ref([
  { label: '≤', value: 0 }, { label: '<', value: 1 },
])
const intensityOption = ref([
  { label: 'IV度', value: 4 }, { label: 'V度', value: 5 },
  { label: 'VI度', value: 6 }, { label: 'VII度', value: 7 },
  { label: 'VIII度', value: 8 }, { label: 'IX度', value: 9 },
  { label: 'X度', value: 10 }, { label: 'XI度', value: 11 },
  { label: 'XII度', value: 12 },
])
const formRules = reactive({
  resLevel: [{ required: true, message: '级别不能为空', trigger: 'change' }],
  resLevelName: [{ required: true, message: '级别名称不能为空', trigger: 'blur' }],
  regionId: [{ required: true, message: '区域不能为空', trigger: 'change' }],
  distance: [{ required: true, message: '海岸线及边境区域外扩距离不能为空', trigger: 'blur' }],
  identifying: [{ required: true, message: '启用标识不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref
const disabled = ref(false) //查看禁用

const open = async (type, id) => {
  resetForm()
  disabled.value = (type == 'detail')
  formType.value = type
  await getRegion()
  // 修改时，设置数据
  if (id) {
    // 获得区域列表
    formLoading.value = true
    try {
      formData.value = await levelApi.getResLevelConf(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success', 'setClose']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    if (formType.value == 'update') {
      await levelApi.saveResLevelConf({ ...formData.value })
      message.success(t('common.updateSuccess'))
    } else {
      await levelApi.saveResLevelConf({ ...formData.value })
      message.success(t('common.createSuccess'))
    }
    // 发送操作成功的事件
    emit('success')
    setClose()
  } finally {
    formLoading.value = false
  }
}
const setClose = () => {
  emit('setClose')
}
/** 获取区域的数据  */
const regionList = ref([] as any[]) // 树形结构
const getRegion = async () => {
  regionList.value = await levelApi.getAllRegion()
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: null,
    resLevel: '',
    resLevelName: '',
    regionId: '',
    distance: 0,
    range1: undefined,
    symbol1: 0,
    symbol2: 0,
    range2: undefined,
    depth1: undefined,
    symbol3: 0,
    symbol4: 0,
    depth2: undefined,
    intensity1: '',
    symbol5: 0,
    symbol6: 0,
    intensity2: '',
    population1: undefined,
    symbol7: 0,
    symbol8: 0,
    population2: undefined,
    identifying: 0,
  }
  formRef.value?.resetFields()
}
</script>
